-module(fbw).
-compile(export_all).

times(X) ->
  fun(Value) -> Value rem X =:= 0 end.

contain(X) ->
    fun(Value) ->
         lists:member(X,integer_to_list(Value)) end.

rule(Pred, Action) ->
     fun(Value) ->
             case Pred(Value) of
                 true -> {true, Action(Value)};
                 false -> false
             end
     end.
'OR'([])-> fun(_)->false end;
'OR'([H|T])->
    fun(Value)->
            case H(Value) of
                {true,Action} -> {true, Action};
                false -> ('OR'(T))(Value)
            end
    end.


%% 'AND'(Rules)->
%%     'AND'(Rules
%%          , fun(A1,A2)->
%%                    A1++A2 end
%%          , "").
%% 'AND'([], _, Acc)->
%%     fun(_)-> {true,Acc} end;
%% 'AND'([H|T], Op, Acc) ->
%%     fun (N) ->  
%%             case H(N) of
%%                 {true, A}->('AND'(T,Op,Op(A,Acc)))(N);
%%                 false ->false
%%             end
%%     end.

'AND'([])-> fun(_)->{true,""} end;
'AND'([H|T])->
    fun(Value)->
            case H(Value) of
               {true,Action} -> ('AND'(T))(Value);
                false -> false
            end
    end.

game()->
    R1_3=rule(times(3), fun(_)->"f" end),
    R1_5=rule(times(5), fun(_)->"b" end),
    R1_7=rule(times(7), fun(_)->"w" end),
    R1='OR'([R1_3,R1_5,R1_7]),
    R2='OR'(['AND'([R1_3,R1_5,R1_7]),
            'AND'([R1_3,R1_5]),
            'AND'([R1_5,R1_7]),
             'AND'([R1_3,R1_5])]),
    R3=rule(contain(3), fun(_)->"f" end),
    Rd='OR'([rule(fun(_)->true end, fun(N)->N end)]),
    Spec='OR'([R3,R2,R1,Rd]), 
    Rt='OR'(['OR'([R1_5, R1_7])]),
%%    Rt(35).
    [Spec(I)||I<-lists:seq(1,100)].
    
